// src/core/module-types.ts

import { RouteRecordRaw } from 'vue-router';
import { ModuleId, ServiceKey, ModuleDependency, ModuleConfig } from './types';
import type { Store } from 'pinia';

/**
 * 模块上下文接口
 */
export interface ModuleContext {
  router: Router;
  pinia: Pinia;
  config: ModuleConfig;
  
  // 状态管理
  state: Map<string, Store>;
  
  // 服务管理
  services: {
    register<T>(key: ServiceKey<T>, service: T): void;
    get<T>(key: ServiceKey<T>): T | undefined;
    has(key: ServiceKey<any>): boolean;
  };
  
  // 事件通信
  events: {
    emit<T extends EventData>(event: string, data: T): void;
    on<T extends EventData>(event: string, handler: (data: T) => void): void;
  };
  
  // 模块间通信
  modules: {
    send<T>(target: ModuleId, message: T): Promise<boolean>;
    receive<T>(handler: (message: T, sender: ModuleId) => void): void;
  };
  
  // 日志接口
  logger: {
    info(message: string, ...args: any[]): void;
    warn(message: string, ...args: any[]): void;
    error(message: string, ...args: any[]): void;
    debug(message: string, ...args: any[]): void;
  };
}

/**
 * 模块生命周期钩子
 */
export interface ModuleLifecycle {
  onMount?(ctx: ModuleContext): Promise<void> | void;
  onUnmount?(): Promise<void> | void;
}

/**
 * 模块功能定义
 */
export interface ModuleFeatures {
  /**
   * 注册模块路由
   */
  registerRoutes(): RouteRecordRaw | RouteRecordRaw[];
  
  /**
   * 注册Pinia状态
   */
  registerStores?(): Record<string, () => Store>;
  
  /**
   * 注册全局服务
   */
  registerServices?(): Record<ServiceKey<any>, () => any>;
}

/**
 * 完整的模块定义接口
 */
export interface ModuleDefinition 
  extends ModuleLifecycle, ModuleFeatures {
  id: ModuleId;
  version: string;
  dependencies?: ModuleDependency[];
  signature?: string;
}